#include "cmdfashion.h"
#include "commmsg.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_FASHION_OPT, CCmdFashionOpt);
int CCmdFashionOpt::Do(void* pData)
{
    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_FASHION_OPT ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_FASHION_OPT,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    //检查登录
    _uiGID = stReq.gid();
    iRet = pstMainCtrl->CheckLogin(_uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdFashionOpt::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, _uiGID);
    PREPARE_PLAYER_DETAIL_EX1_INFO(this, _uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, _uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", _uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo =
        pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_NO_USER);
    }
    CPlayerOnlineData& stPlayerOnlineData =
        pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    if (!pstMainCtrl->m_stCommConfigLoader.IsOpen(SYS_OPEN_COND_ID_26,
            stPlayerBaseInfo, stPlayerOnlineData))
    {
        return AnsClients(ERROR_SYS_NOT_OPEN);
    }

    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("gid = %u", _uiGID));
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerDetailInfoEx1& stPlayerDetailEx1 =
        pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

    CUserFashion& stUserFashion = stPlayerDetailEx1._stUserFasion;
    bool bNeedSaveBaseInfo  = false;
    bool bNeedSaveDetailEx1 = true;
    bool bNeedSendWingBag = false;
    bool bNeedSendDressBag = false;

    unsigned int uiNotify = 0;

    switch (stReq.opt())
    {
    case CFashionMgr::CMD_FASHION_SUB_GET_INFO:  //获取信息
        {
            bNeedSaveDetailEx1 = false;
        }
        break;

    case CFashionMgr::CMD_FASHION_SUB_WARE: //穿时装
        {
            if (stReq.fashion_id() == (int)stUserFashion._uiNowWing)
            {
                pstMainCtrl->m_stFashionMgr.GetFashionInfo(stUserFashion,
                    stPlayerOnlineData, stAns);

                return AnsClients(RET_OK);
            }
            
            iRet = pstMainCtrl->m_stFashionMgr.UseFashion(stUserFashion,
                stPlayerOnlineData, stReq.fashion_id());
            if (iRet == RET_OK)
            {
                uiNotify |= NOTIFY_MAP_INFO;
                uiNotify |= NOTIFY_ROLE_ATTR;
                bNeedSendWingBag = false;
            }
        }
        break;

    case CFashionMgr::CMD_FASHION_SUB_SHOW_WING: //显示翅膀
    case CFashionMgr::CMD_FASHION_SUB_HIDE_WING: //隐藏翅膀
    case CFashionMgr::CMD_FASHION_SUB_SHOW_DRESS: //显示装扮
    case CFashionMgr::CMD_FASHION_SUB_HIDE_DRESS: //隐藏装扮
        {
            iRet = pstMainCtrl->m_stFashionMgr.HiddenFashion(stUserFashion,
                stPlayerOnlineData, stReq.opt());
            if (iRet == RET_OK)
            {
                uiNotify |= NOTIFY_MAP_INFO;
            }
        }
        break;

    case CFashionMgr::CMD_FASHION_SUB_EXTEND: //时装续费
        {
            int iFasionPos = 0;
            iRet = pstMainCtrl->m_stFashionMgr.ExtendValidity(stPlayerBaseInfo,
                stPlayerOnlineData, stUserFashion, stReq.fashion_id(), iFasionPos);        
            if (iRet == RET_OK)
            {
                uiNotify |= NOTIFY_USER_MONEY;
                uiNotify |= NOTIFY_ROLE_ATTR;

                if (FASION_POS_WING == iFasionPos)
                {
                    bNeedSendWingBag = true;
                }
                else if (FASION_POS_DRESS == iFasionPos)
                {
                    bNeedSendDressBag = true;
                }
            }
        }
        break;

    case CFashionMgr::CMD_FASHION_SUB_TAKE_OFF: //脱时装
        {
            iRet = pstMainCtrl->m_stFashionMgr.UnWareFashion(stUserFashion,
                stReq.fashion_id());        
            if (iRet == RET_OK)
            {
                uiNotify |= NOTIFY_MAP_INFO;
                uiNotify |= NOTIFY_ROLE_ATTR;
            }
        }
        break;

    default:
        return AnsClients(ERROR_FASION_OPT);
    }

    if (iRet)
    {
        return AnsClients(iRet);
    }

    //保存数据
    if (bNeedSaveBaseInfo)
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_BASE_INFO;
        stKey.m_u64ID = stPlayerBaseInfo._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);
    }

    if (bNeedSaveDetailEx1)
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_DETAIL_EX1_INFO;
        stKey.m_u64ID = stPlayerDetailEx1._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerDetailEx1, VOP_UPDATE);
    }
        
    pstMainCtrl->m_stFashionMgr.InitData(stUserFashion, stPlayerOnlineData);
    pstMainCtrl->m_stNotifyMgr.Notify(stPlayerBaseInfo._uiGID, uiNotify);
    if (bNeedSendWingBag)
    {
        pstMainCtrl->m_stFashionMgr.SendWingBagInfoToFront(stUserFashion,
            _uiGID, stMsgHead, stEnpHead);
    }
    if (bNeedSendDressBag)
    {
        pstMainCtrl->m_stFashionMgr.SendDressBagInfoToFront(stUserFashion,
            _uiGID, stMsgHead, stEnpHead);
    }
    
    pstMainCtrl->m_stFashionMgr.GetFashionInfo(stUserFashion,
        stPlayerOnlineData, stAns);

    return AnsClients(RET_OK);
}

int CCmdFashionOpt::AnsClients(int iRet)
{
    stMsgHead.set_msgret(iRet);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    stAns.set_opt(stReq.opt());    
    pstMainCtrl->m_stEncodeBuf.Clear();
    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);
    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING((""));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

